package com.smart.manager.weixin.mp.handler;

import com.smart.common.exception.SmartException;
import com.smart.manager.weixin.mp.repository.WxmpAccountRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.stereotype.Service;

/**
 * @author Xiaxuqiu
 * @date 2023-05-13
 **/
@Service @Log4j2
@RequiredArgsConstructor
public class WxmpQrcodeHandler {

    private final WxMpService wxMpService;
    private final WxmpAccountRepository accountRepository;

    public String getQrCodeStr(String text, String appId) throws WxErrorException {

            WxMpService mpService = wxMpService.switchoverTo(appId);
            WxMpQrcodeService qrcodeService =mpService.getQrcodeService();
            WxMpQrCodeTicket wxMpQrCodeTicket = qrcodeService.qrCodeCreateTmpTicket(text, 60 * 60 * 24 * 7);
            log.warn(wxMpQrCodeTicket);
            return wxMpQrCodeTicket.getUrl();

    }


    /**
     * @param text 获取默认公众号的二维码
     * @return {@link String}
     */
    public String getQrCodeStrFormDefault(String text) throws WxErrorException {
        String defaultAppId = accountRepository.getDefaultAppId();
        return getQrCodeStr(text, defaultAppId);

    }
}
